import fs from "fs"
import { dirname, resolve } from 'path'
import { fileURLToPath } from 'url'

const CONFIG_PATH = "./config.env"
const CONFIG_ABSOLUTE_PATH = resolve(dirname(fileURLToPath(import.meta.url)), CONFIG_PATH)

const result = fs.readFileSync(CONFIG_ABSOLUTE_PATH, { encoding: "utf-8" })
const reg = /[^=\n]+/g
const matchArr = result.match(reg) || []
const len = matchArr.length

if (len % 2 !== 0) {
  throw Error('Invalid configuration')
}

for (let i = 0; i < len; i++) {
  const key = matchArr[i]
  const val = matchArr[++i].replaceAll('"', "")
  if (!process.env[key]) {
    process.env[key] = val
  }
}

console.log(`✔ : config inject success`);

